package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        List<NursingProjectPlan> list = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> collect = list.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
            nursingProjectPlanVo.setId(Long.valueOf(projectPlan.getId()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(projectPlan.getPlanId()));
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(projectPlan.getExecuteFrequency()));

            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(collect);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        this.save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean saveBatch = iNursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(CACHE_KEY);
        return saveBatch?1:0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean saveBatch = iNursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(CACHE_KEY);
        return saveBatch?1:0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        if (nursingPlanMapper.selectNursingPlanById(id) != null) {
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        }
        boolean result = removeById(id);
        redisTemplate.delete(CACHE_KEY);
        return  result? 1 : 0;
    }

    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY);
        if(list != null && list.size() > 0){
            return list;
        }
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectList(Wrappers.<NursingPlan>lambdaQuery().eq(NursingPlan::getStatus, 1));
        redisTemplate.opsForValue().set(CACHE_KEY, nursingPlans);
        return nursingPlans;
    }
}
